/**
 * 
 */
package stepdefinations;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import selenium.SeleniumBase;
import cucumber.api.DataTable;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

/**
 * @author Pallavi
 *
 */
public class SitemapSearchRegression extends SeleniumBase {
	

	@Given("^As a user I click on \"(.*?)\" link$")
	public void asauserIclickonsitemaplink(String link) throws Throwable {
		Assert.assertTrue("could not find link to click",isSitemapLinkVisible(link));
	    
	}

	private boolean isSitemapLinkVisible(String link) {
		JavascriptExecutor jse = (JavascriptExecutor) driver;
		jse.executeScript("scroll(0, 500);");
		driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
		if (driver.findElement(By.partialLinkText(link)).isDisplayed()) {
			driver.findElement(By.partialLinkText(link)).click();
			
		return true;
	}
return false;
}

	
	@Given("^I tick the check box \"(.*?)\"$")
	public void itickthecheckbox(String advancedsearch) throws Throwable {
		Assert.assertTrue(isAdvancedSearchcheckboxVisible(advancedsearch));
	    
	}

	private boolean isAdvancedSearchcheckboxVisible(String advancedsearch) {
		if(!driver.findElement(By.id(advancedsearch)).isSelected()){
			driver.findElement(By.id(advancedsearch)).click();
			return true;
		}
			return false;
			
	}

	@Then("^I tick the  \"(.*?)\",\"(.*?)\"$")
	public void itickth_checkbox(String Automaticallysearchsubcategories, String SearchInproductdescriptions) throws Throwable {
		Assert.assertTrue(isOtherTwoCheckboxesVisible(Automaticallysearchsubcategories,SearchInproductdescriptions));
	   
	}

	private boolean isOtherTwoCheckboxesVisible(
			String automaticallysearchsubcategories,
			String searchInproductdescriptions) {
	String checkbox[] = {"automaticallysearchsubcategories","searchInproductdescriptions"};
		
		for (int i = 0; i < checkbox.length; i++) {
	    	if(!driver.findElement(By.id(checkbox[i])).isSelected()){
	    	driver.findElement(By.id(checkbox[i])).click();	
	    	driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
	    }
	    	return true;
	}
		return false;
	}

	
	
	@Then("^I fill textbox \"(.*?)\",\"(.*?)\",\"(.*?)\"$")
	public void i_fill_textbox(String searchfield,String startprice,String MaxPrice,List<String> results) throws Throwable {
		for (String element : results) {
			if (element.equalsIgnoreCase("search-text")) {
				 driver.findElement(By.className(element)).sendKeys(searchfield);
			}else if (element.equalsIgnoreCase("price-from"))
			{
				driver.findElement(By.className(element)).sendKeys(startprice);	
							}
			else if (element.equalsIgnoreCase("price-to")) {
				driver.findElement(By.className(element)).sendKeys(MaxPrice);
			}
			   
			   driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
			   
		   }
	}

	@Then("^I tick the check box$")
	public void i_tick_textbox(List<String> results) throws Throwable {
		for (String string : results) {
			   driver.findElement(By.id(string)).click();
			   driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
			   
		   }
		
	}
	
	

	@Given("^I select category \"(.*?)\" Manufacturer \"(.*?)\"$")
	public void i_select_category_Manufacturer(String arg1, String arg2) throws Throwable {
	   
	}

	@When("^I click on search button$")
	public void iclickonsearchbutton() throws Throwable {
		Assert.assertTrue(isSearchButtonVisible());
	   
	}

	private boolean isSearchButtonVisible() {
		if (driver.findElement(By.cssSelector("input.button-1.search-button")).isDisplayed()) {
			driver.findElement(By.cssSelector("input.button-1.search-button")).click();
			driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
			return true;
		}
		return false;
	}

	@Then("^I should see the \"(.*?)\" displayed$")
	public void ishouldseethesearchresultsdisplayed(String text) throws Throwable {
		Assert.assertEquals(text,VerifyTheResultsDisplayed());
	   
	}

	private String VerifyTheResultsDisplayed() {
		return 	driver.findElement(By.linkText("HP Pavilion Artist Edition DV2890NR 14.1-inch Laptop")).getText();
		
	}
}
	
	